﻿/* ============================================================================
(c) Copyright 2012 Hewlett-Packard Development Company, L.P.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge,publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
============================================================================ */
// =====================================================================================
// A NOTE TO TESTERS..
// =====================================================================================
// These unit test require a particular directory structure to be present on your machine
// to complete the test cases where we copy files and or folders to the server. Make sure
// that you have something like the following on your machine.
//
// Folder1 (contains files)
//     ->  Folder2 (contains files)
//              -> Folder3 (contains NO files)
//
// Note : Go to the Testing section in the CLI.Config file. Make sure that LocalTestDirectory 
// element points to the root of the test folder hierarchy (Folder1 in the example above).
// =====================================================================================

using Microsoft.VisualStudio.TestTools.UnitTesting;
using HPCloud.Objects.DataAccess;
using HPCloud.Objects.Domain;
using HPCloud.Objects.Domain.Admin;
using System.IO;

namespace HPCloud.Objects.Test.Unit_Test.ObjectStorage
{
    [TestClass]
    public class ObjectCopyTest : StorageTest
    {
        const string cDelimiter = "/";
//=======================================================================================================
/// <summary>
/// copy c:\documents\Anothertest.txt testcon\d1\anothertest.txt
/// </summary>
//=======================================================================================================
        [TestMethod]
        public void Copy_Folder_LocalToServer_Recursive()
        {
            // Set up our test environment first..

            this.ResetTestData();

            // Get our target and source paths in order..

            string targetPath = this.StorageURL + "/testcon/d1/";
            string sourcePath = this.Context.Settings.LocalTestDirectory;

            // Do the copy..

            IStorageObjectRepository repository = this.Factory.CreateStorageObjectRepository();
            repository.Copy(sourcePath, targetPath, true);

            // Test the results. Make sure the file got copied correctly.

            StorageObject file = repository.GetStorageObject(this.StorageURL + "/testcon/d1/Testing/Anothertest.txt");
            Assert.IsNotNull(file);
        }
//=======================================================================================================
/// <summary>
/// copy c:\documents\Anothertest.txt testcon\d1\anothertest.txt
/// </summary>
//=======================================================================================================
        [TestMethod]
        public void Copy_Folder_LocalToServer_NonRecursive()
        {
            // Set up our test environment first..

            this.ResetTestData();

            // Get our target and source paths in order..

            string targetPath = this.StorageURL + "/testcon/d1/";
            string sourcePath = this.Context.Settings.LocalTestDirectory;

            // Do the copy..

            IStorageObjectRepository repository = this.Factory.CreateStorageObjectRepository();
            repository.Copy(sourcePath, targetPath, false);

            // Test the results. Make sure the file got copied correctly.

            StorageObject file = repository.GetStorageObject(this.StorageURL + "/testcon/d1/Testing/Anothertest.txt");
            Assert.IsNotNull(file);
        }
//=======================================================================================================
/// <summary>
        /// copy c:\documents\Anothertest.txt testcon\d1\anothertest.txt
/// </summary>
//=======================================================================================================
        [TestMethod]
        public void Copy_Object_LocalToServer()
        {
            // Set up our test environment first..

            this.ResetTestData();

            // Get our target and source paths in order..

            string targetPath = this.StorageURL + "/testcon/d1/Anothertest.txt";
            string sourcePath = this.Context.Settings.TestFilePath;

            // Do the copy..

            IStorageObjectRepository repository = this.Factory.CreateStorageObjectRepository();
            repository.Copy(sourcePath, targetPath, false);

            // Test the results. Make sure the file got copied correctly.

            StorageObject file = repository.GetStorageObject(this.StorageURL + "/testcon/d1/Anothertest.txt");
            Assert.IsNotNull(file);
        }
//=======================================================================================================
/// <summary>
/// From Root -> Copy anothertest.txt d1\anothertest.txt
/// </summary>
//=======================================================================================================
        [TestMethod]
        public void Copy_Object_IntraServer()
        {
            // Set up our test environment first..

            this.ResetTestData(true);

            // Get our target and source paths in order..

            string sourcePath  = this.StorageURL + "/testcon/Anothertest.txt";
            string targetPath  = this.StorageURL + "/testcon/d1/Anothertest.txt";

            // Load the local file up and copy it over to the server..

            IStorageObjectRepository repository = this.Factory.CreateStorageObjectRepository();
            repository.Copy(sourcePath, targetPath, false);

            // Test the results. Make sure the file got copied correctly.

            string testPath = this.StorageURL + "/testcon/d1/Anothertest.txt";
            StorageObject file = repository.GetStorageObject(testPath);
            Assert.IsNotNull(file);    
        }
//=======================================================================================================
/// <summary>
/// From Root -> Copy anothertest.txt d1\anothertest.txt
/// </summary>
//=======================================================================================================
        [TestMethod]
        public void Copy_Folder_IntraServer_NonRecursive()
        {
            // Set up our test environment first..

            this.ResetTestData(true);

            // Get our target and source paths in order..

            string sourcePath = this.StorageURL + "/testcon/";
            string targetPath = this.StorageURL + "/testcon/d1/";

            // Load the local file up and copy it over to the server..

            IStorageObjectRepository repository = this.Factory.CreateStorageObjectRepository();
            repository.Copy(sourcePath, targetPath, false);

            // Test the results. Make sure the file got copied correctly.

            string testPath = this.StorageURL + "/testcon/d1/Anothertest.txt";
            StorageObject file = repository.GetStorageObject(testPath);
            Assert.IsNotNull(file);
        }
//=======================================================================================================
/// <summary>
/// From Root -> Copy anothertest.txt d1\anothertest.txt
/// </summary>
//=======================================================================================================
        [TestMethod]
        public void Copy_Folder_IntraServer_Recursive()
        {
            // Set up our test environment first..

            this.ResetTestData(true);

            // Get our target and source paths in order..

            string sourcePath = this.StorageURL + "/testcon/";
            string targetPath = this.StorageURL + "/testcon/d1/d2/";

            // Load the local file up and copy it over to the server..

            IStorageObjectRepository repository = this.Factory.CreateStorageObjectRepository();
            repository.Copy(sourcePath, targetPath, true);

            // Test the results. Make sure the file got copied correctly.

            string testPath = this.StorageURL + "/testcon/d1/d2/Anothertest.txt";
            StorageObject file = repository.GetStorageObject(testPath);
            Assert.IsNotNull(file);
        }
//=======================================================================================================
/// <summary>
/// From testcon:\d1 - > Copy d1\Anothertest.txt e:\Projects\Testing\
/// </summary>
//=======================================================================================================
        [TestMethod]
        public void Copy_Object_ServerToLocal()
        {
            // Set up our test environment first..

            this.ResetTestData(true);

            // Get our target and source paths in order..
           
            string sourcePath = this.StorageURL + "/testcon/Anothertest.txt";
            string targetPath = this.Settings.LocalTestDirectory + @"t1\Anothertest.txt";

            // Load the local file up and copy it over to the server..

            IStorageObjectRepository repository = this.Factory.CreateStorageObjectRepository();
            repository.Copy(sourcePath, targetPath, false);

            // Test the results. Make sure the file got copied correctly.

            Assert.IsTrue(File.Exists((this.Settings.LocalTestDirectory + @"t1\Anothertest.txt")));
        }
//=======================================================================================================
/// <summary>
/// From testcon:\d1 - > Copy d1\Anothertest.txt e:\Projects\Testing\
/// </summary>
//=======================================================================================================
        [TestMethod]
        public void Copy_Folder_ServerToLocal_Recursive()
        {
            // Set up our test environment first..

            this.ResetTestData(this.StorageURL + "/testcon/d1/d2/Anothertest.txt");

            // Get our target and source paths in order..

            string sourcePath = this.StorageURL + "/testcon/";
            string targetPath = this.Settings.LocalTestDirectory + @"t1\";

            // Load the local file up and copy it over to the server..

            IStorageObjectRepository repository = this.Factory.CreateStorageObjectRepository();
            repository.Copy(sourcePath, targetPath, true);

            // Test the results. Make sure the file got copied correctly.

            Assert.IsTrue(File.Exists((this.Settings.LocalTestDirectory + @"t1\d1\d2\Anothertest.txt")));
        }
//=======================================================================================================
/// <summary>
/// From testcon:\d1 - > Copy d1\Anothertest.txt e:\Projects\Testing\
/// </summary>
//=======================================================================================================
        [TestMethod]
        public void Copy_Folder_ServerToLocal_NonRecursive()
        {
            // Set up our test environment first..

            this.ResetTestData(this.StorageURL + "/testcon/d1/Anothertest.txt");

            // Get our target and source paths in order..

            string sourcePath = this.StorageURL + "/testcon/d1/";
            string targetPath = this.Settings.LocalTestDirectory + @"t1\";

            // Load the local file up and copy it over to the server..

            IStorageObjectRepository repository = this.Factory.CreateStorageObjectRepository();
            repository.Copy(sourcePath, targetPath, false);

            // Test the results. Make sure the file got copied correctly.

            Assert.IsTrue(File.Exists((this.Settings.LocalTestDirectory + @"t1\d1\Anothertest.txt")));
        }  
    }
}


